package com.easyJava.builder;

import com.easyJava.bean.Constants;
import com.easyJava.bean.FieldInfo;
import com.easyJava.utils.BufferedWriterUtils;
import com.easyJava.utils.LocalDateTimeUtils;
import com.easyJava.utils.LocalDateUtils;
import org.apache.commons.lang3.ArrayUtils;

import java.io.BufferedWriter;
import java.io.IOException;

public class BuildAnnotation {

    public static void createFieldAnnotation(BufferedWriterUtils bw, FieldInfo fieldInfo) throws IOException {
        if(ArrayUtils.contains(Constants.IGNORE_FIELDS, fieldInfo.getFieldName())){
            bw.write("\t" + Constants.IGNORE_FIELD_EXPRESSION);
        }
        if(ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPE, fieldInfo.getSqlType())){
            bw.write("\t" + String.format(Constants.DATE_FORMAT_EXPRESSION, LocalDateTimeUtils.YYYY_MM_DD_HH_MM_SS));
            bw.write("\t" + String.format(Constants.DATE_JSON_FORMAT_EXPRESSION, LocalDateTimeUtils.YYYY_MM_DD_HH_MM_SS));
        }
        if(ArrayUtils.contains(Constants.SQL_DATE_TYPE, fieldInfo.getSqlType())){
            bw.write("\t" + String.format(Constants.DATE_FORMAT_EXPRESSION, LocalDateUtils.YYYY_MM_DD));
            bw.write("\t" + String.format(Constants.DATE_JSON_FORMAT_EXPRESSION, LocalDateUtils.YYYY_MM_DD));
        }
    }

    public static void createQueryFieldAnnotation(BufferedWriterUtils bw, FieldInfo fieldInfo) throws IOException {
        if(ArrayUtils.contains(Constants.SQL_DATE_TIME_TYPE, fieldInfo.getSqlType())){
            bw.write("\t" + String.format(Constants.DATE_FORMAT_EXPRESSION, LocalDateTimeUtils.YYYY_MM_DD_HH_MM_SS));
        }
        if(ArrayUtils.contains(Constants.SQL_DATE_TYPE, fieldInfo.getSqlType())){
            bw.write("\t" + String.format(Constants.DATE_FORMAT_EXPRESSION, LocalDateUtils.YYYY_MM_DD));
        }
    }
}
